import string, threading, time

#method one
class MyThread(threading.Thread):
    def __init__(self, num):
        #threading.Thread.__init__(self)
        super(MyThread, self).__init__()
        self._run_num = num

    def run(self):
        global mutex
        mutex.acquire() 
        print 'thread%d run' % self._run_num 
        mutex.release()

#method two
def thread_main(a):
    global mutex
    mutex.acquire() 
    print 'thread%d run' % a 
    mutex.release()

threads = []
mutex = threading.Lock()

for x in xrange(0, 4):
    threads.append(threading.Thread(target=thread_main, args=(x,)))

for t in threads:
    t.start()

for t in threads:
    t.join()

threads = []
for x in xrange(0, 4):
    threads.append(MyThread(x)) 
for t in threads:
    t.start()

for t in threads:
    t.join()


